package com.example.user.controller.permission;

import com.example.framework.common.pojo.R;
import com.example.user.controller.permission.vo.permission.PermissionAssignRoleMenuReqVO;
import com.example.user.controller.permission.vo.permission.PermissionAssignUserRoleReqVO;
import com.example.user.service.permission.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@Tag(name = "权限管理")
@RestController
@RequestMapping("/system/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @Operation(summary = "获得角色拥有的菜单编号")
    @Parameter(name = "roleId", description = "角色编号", required = true)
    @GetMapping("/list-role-menus")
    @PreAuthorize("hasAuthority('system:permission:assign-role-menu')")
    public R<Set<Long>> getRoleMenuList(Long roleId) {
        return R.success(permissionService.getRoleMenuListByRoleId(roleId));
    }

    @PostMapping("/assign-role-menu")
    @Operation(summary = "赋予角色菜单")
    @PreAuthorize("hasAuthority('system:permission:assign-role-menu')")
    public R<Boolean> assignRoleMenu(@Validated @RequestBody PermissionAssignRoleMenuReqVO reqVO) {
        // 执行菜单的分配
        permissionService.assignRoleMenu(reqVO.getRoleId(), reqVO.getMenuIds());
        return R.success(true);
    }

    @Operation(summary = "重新赋予用户角色")
    @PostMapping("/assign-user-role")
    @PreAuthorize("hasAuthority('system:permission:assign-user-role')")
    public R<Boolean> assignUserRole(@Validated @RequestBody PermissionAssignUserRoleReqVO reqVO) {
        permissionService.assignUserRole(reqVO.getUserId(), reqVO.getRoleIds());
        return R.success(true);
    }
}
